// *******************登录页面的接口
import { getCapture, login } from '@/services';
import { removeCookie, setCookie } from '@/utils/bauch';
import { ILoginData } from '@/utils';
import { makeAutoObservable } from 'mobx'

interface ICaptureImage{
    img: string,
    uuid: string
}
class User{
    constructor() {
        makeAutoObservable(this);
    }
    captchaImage: ICaptureImage= {} as ICaptureImage;
    authorization: string = '';

    async getCapture(){
        let result = await getCapture();
        if (result.code === 200){
            let {img, uuid} = result
            this.captchaImage = {img, uuid};
        }
    }

    async login(data: ILoginData){
        let result = await login({...data, uuid: this.captchaImage.uuid})
        if (result.code === 200){
            let authorization = this.authorization = `Bearer ${result.token}`;
            setCookie(authorization);
            return authorization;
        }
    }

    logout(){
        removeCookie();
        this.authorization = '';
    }    
}

export default new User;